package com.gxa.utils;

import io.swagger.annotations.*;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @author bq
 * Date: 2020/11/02 8:39
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("upload")
@Api(value = "文件上传",tags = "文件上传接口API")
public class UploadFileController {
    @Autowired
    private ResponseData responseData;
    /**
     * 多文件上传
     * @param file
     * @return
     */
    @PostMapping("photos")
    @ApiOperation(value = "多文件上传",notes = "多文件上传接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="上传成功"),
            @ApiResponse(code=400,message="上传失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData uploadPhotos(HttpServletRequest request, @ApiParam(value = "图片", required = true) MultipartFile[] file) throws IOException, FileUploadException {
        String[] src = UploadFileUtil.uploadFile(request, file);
        if(src.equals("")&&src==null){
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("上传失败");
            responseData.setData(src);
        }else {
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("上传成功");
            responseData.setData(src);
        }
        return responseData;
    }
}
